package cn.chinaflame.pagodas.web;

import cn.chinaflame.pagodas.Result.Requests;
import cn.chinaflame.pagodas.aop.WebHandleInterceptor;
import cn.chinaflame.pagodas.constant.Sysconst;
import cn.chinaflame.pagodas.entity.PaAgent;
import cn.chinaflame.pagodas.enums.CodeEnum;
import cn.chinaflame.pagodas.utils.ParamPreconditions;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Component
public class SecurityCheckInterceptor implements WebHandleInterceptor {

    private static final Logger LOG = LoggerFactory.getLogger(SecurityCheckInterceptor.class);

    @Override
    public int getOrder() {
        return 0;
    }

    @Override
    public void preHandle(MethodSignature methodSignature) {
        Method method = methodSignature.getMethod();
        RestAction ra = method.getAnnotation(RestAction.class);
        Requests.printCookie();
        if (ra != null) {
            LOG.info("代理安全验证");
            PaAgent current = SessionFactory.getUserObj(Requests.getSessionIdByCookie(), Sysconst.USER_PAGODA_AGENT, PaAgent.class);
            ParamPreconditions.checkNotNull(current, CodeEnum.SESSION_INVLID.getCode(), CodeEnum.SESSION_INVLID.getMsg());
        }
    }
}
